$(function() {


    // var tit_user = null;
    // console.log(getCookie('user'));
    var tit_user = getCookie("user");
    if (tit_user) {
        $(".adad").hide();
        $(".hy").show().text(`${tit_user},欢迎你`);
        $(".hy").click(function() {
            if (confirm("确定退出登录吗?")) {
                setCookie("user", `${tit_user}`, day = -1, path = "/");
                $(".adad").show();
                $(".hy").hide();
            }
        })
    }


    //判断是否有cookie 并跳转购物车页面
    $(".shopCart a").click(function() {
        if (document.cookie) {
            location.href = "../html/shoppingCar.html";
        } else {
            alert("请先登录");
        }
    })

    //判断是否有cookie 并跳我的收藏页面
    $(".nav_list li").eq(3).click(function() {
        if (document.cookie) {

            location.href = "../html/myLikeGoods.html";

        } else {
            alert("请先登录");
        }
    })
    $(".register").click(function() { //点击注册框出现
        $(".registerBox").show()
        $("#mc").show();
    })

    $(".registerBox_dl").click(function() { //点击注册框消失
        $(".registerBox").hide()
        $("#mc").hide();
        $(".zhuce").css({ border: "1px solid #dcdfe6" });
        $(".pwds").css({ border: "1px solid #dcdfe6" });
        $(".pwdAgain").css({ border: "1px solid #dcdfe6" });

    })

    $(".login").click(function() { //点击登录框出现
        $(".loginBox").show()
        $("#mc").show();

    })

    $(".loginBox_dl").click(function() { //点击登录框消失
        $(".loginBox").hide();
        $("#mc").hide();
    })

    var isUserOk = false;
    var isPwdOk = false;
    var isPwdAgain = false;

    // 用户名:   由数字,字母 下划线组成,6-12位,且不能以数字开头
    $(".zhuce_zhanghao").blur(function() {
        var user = $(".zhuce_zhanghao").val().trim();
        isUserOk = false;
        var reg = /^[a-zA-Z_]\w{5,11}$/;
        if (reg.test(user)) {
            var xhr = new XMLHttpRequest();
            xhr.open("get", `../php/isExistUser.php?user=${user}`, true);
            xhr.send();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var result = xhr.responseText;
                    result = JSON.parse(result);
                    var { status, msg } = result;
                    if (status) {
                        $(".zhuce").css({ border: "1px solid green" })
                        isUserOk = true;
                    } else {
                        $(".zhuce").css({ border: "1px solid red" })
                        alert(msg);
                    }
                }
            }
        } else {
            $(".zhuce").css({ border: "1px solid red" })
        }
    })

    // 密码:   由数字,字母 下划线组成,6-12位,
    $(".zhuce_pwd").blur(function() {
        var pwd = $(".zhuce_pwd").val().trim();
        isPwdOk = false;
        var reg = /^\w{6,12}$/;
        if (reg.test(pwd)) {
            $(".pwds").css({ border: "1px solid green" })
            isPwdOk = true;
        } else {
            $(".pwds").css({ border: "1px solid red" })
        }


        // 再次确认密码:   由数字,字母 下划线组成,6-12位,
        $(".zhuce_pwds").blur(function() {
            var pwdAgain = $(".zhuce_pwds").val().trim();
            isPwdAgain = false;
            console.log(pwdAgain);
            if (pwd) {
                if (pwdAgain == pwd) {
                    $(".pwdAgain").css({ border: "1px solid green" });
                    isPwdAgain = true;
                } else {
                    $(".pwdAgain").css({ border: "1px solid red" });
                }
            } else {
                $(".pwdAgain").css({ border: "1px solid red" });
            }

        })
    })

    $(".enter").click(function() {
        if (isUserOk & isPwdOk & isPwdAgain) {
            var user = $(".zhuce_zhanghao").val().trim();
            var pwd = $(".zhuce_pwd").val().trim();
            var pwdAgain = $(".zhuce_pwds").val().trim();
            console.log(user, pwd, pwdAgain);

            var xhr = new XMLHttpRequest();
            xhr.open("post", "../php/isRegister.php", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(`user=${user}&pwd=${pwd}`);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var result = JSON.parse(xhr.responseText);
                    var { status, msg } = result;
                    if (status) {
                        alert("注册成功");

                        $(".registerBox").hide();
                        $("#mc").hide();
                    } else {
                        alert(msg);
                    }
                }
            }
        } else {
            alert("请按照提示重新输入红色框内的内容")
        }
    })


    $(".dl").click(function() {
        var user = $(".denglu_zhanghao").val();
        var pwd = $(".denglu_pwd").val();
        if (user && pwd) {
            var xhr = new XMLHttpRequest();
            xhr.open("post", "../php/login.php", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(`user=${user}&pwd=${pwd}`);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var result = xhr.responseText;
                    console.log(result);
                    result = JSON.parse(result);
                    console.log(result);
                    var { status, msg } = result;
                    if (status) {
                        alert("登录成功");
                        $(".loginBox").hide();
                        $("#mc").hide()
                        console.log(setCookie);
                        setCookie("user", `${user}`, day = null, path = "/");
                        $(".adad").hide();
                        $(".hy").show().text(`${user},欢迎你`);
                    } else {
                        // alert(msg);
                        alert(msg);
                        // pwdSpan.className = "err";
                    }

                }
            }
        }
    })

    $(".hy").click(function() {
        var user = $(".denglu_zhanghao").val();
        if (confirm("确定退出登录吗?")) {
            console.log(2);
            setCookie("user", `${user}`, day = -1, path = "/");
            $(".adad").show();
            $(".hy").hide();
            // location.reload();
        }
    })

    //渲染数据-手机
    xuanran("华为", ".content_hw");
    xuanran("一加", ".content_yj");
    xuanran("车", ".content_list");


    //跳转到购物车页面前判断是否登录
    var cookie = document.cookie;
    $(".shopCart a").click(function() {
        if (cookie) {
            location.href = "../html/shoppingCar.html";
        }
    })

    //封装渲染数据函数
    function xuanran(keyV, link) {
        var key = keyV;
        $.ajax({
            type: "get",
            url: "../php/isZhuYeLimit.php",
            data: {
                key
            },
            dataType: "json",
            success: function(result) {
                var { status, msg, list } = result;
                var html = "";
                list.forEach(({ goodsName, goodsImg, goodsPrice, id }) => {
                    html += `
                <li data_id=${id}>
                    <img src="${goodsImg}" alt="">
                    <h2 class="s2">${goodsName}</h2>
                    <p>${goodsPrice}</p>
                </li>`
                });
                $(link).append(html);
                var html = "";
                html += `<li><a href="../html/allGoods.html">浏览更多》</a></li>`;
                $(link).append(html);
                $(".shangping_list").append(html);
                // $(".shangping_list").append(html);

                $(".content_hw  li").click(function() {
                    console.log($(this));
                    var goodsid = $(this).attr("data_id");
                    console.log(goodsid);
                    window.location.href = `./goodsDetails.html?id=${goodsid}`;
                })
                $(".content_yj  li").click(function() {
                    console.log($(this));
                    var goodsid = $(this).attr("data_id");
                    console.log(goodsid);
                    window.location.href = `./goodsDetails.html?id=${goodsid}`;
                })
                $(".content_list  li").click(function() {
                    console.log($(this));
                    var goodsid = $(this).attr("data_id");
                    console.log(goodsid);
                    window.location.href = `./goodsDetails.html?id=${goodsid}`;
                })
            }
        })
    }

})